{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'count_score' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-331554986720>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     59\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mscore\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     60\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 61\u001b[1;33m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"文本'今天天气晴朗，我非常开心地买了一束鲜艳的花'的情感得分：\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msentiment_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'今天天气晴朗，我非常开心地买了一束鲜艳的花'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     62\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"文本'天阴沉沉的，荒无人烟的路边是破旧不堪的老房子'的情感得分：\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msentiment_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'天阴沉沉的，荒无人烟的路边是破旧不堪的老房子'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-3-331554986720>\u001b[0m in \u001b[0;36msentiment_score\u001b[1;34m(sentence)\u001b[0m\n\u001b[0;32m     56\u001b[0m     \u001b[0mseg_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcut_word\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msentence\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     57\u001b[0m     \u001b[0msen_word\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnot_word\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdegree_word\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mclassify_words\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseg_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 58\u001b[1;33m     \u001b[0mscore\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcount_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msen_word\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnot_word\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdegree_word\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mseg_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     59\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mscore\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     60\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'count_score' is not defined"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "import jieba\n",
    "def cut_word(sentence):\n",
    "    cutWord = jieba.cut(sentence)\n",
    "    cut_list = []\n",
    "    for i in cutWord:\n",
    "        cut_list.append(i)\n",
    "    stopwords = set()\n",
    "    with open('data/stopwords.txt', 'r', encoding = 'utf-8') as fr:\n",
    "        for i in fr:\n",
    "            stopwords.add(i.strip())\n",
    "    return list(filter(lambda x :x not in stopwords, cut_list))\n",
    "def classify_words(word_list):\n",
    "    sen_file = open('data/BosonNLP_sentiment_score.txt', 'r+', encoding = 'utf-8')\n",
    "    sen_list = sen_file.readlines()\n",
    "    sen_dict = defaultdict()\n",
    "    for i in sen_list:\n",
    "        if len(i.split(' ')) == 2:\n",
    "            sen_dict[i.split(' ')[0]] = i.split(' ')[1]\n",
    "            not_word_file = open('data/否定词.txt', 'r+', encoding = 'utf-8')\n",
    "            not_word_list = not_word_file.readlines()\n",
    "            degree_file = open('data/程度副词.txt', 'r+', encoding = 'utf-8')\n",
    "            degree_list = degree_file.readlines()\n",
    "            degree_dict = defaultdict()\n",
    "            for i in degree_list:\n",
    "                degree_dict[i.split(',')[0]] = i.split(',')[1]\n",
    "            sen_word = dict()\n",
    "            not_word = dict()\n",
    "            degree_word = dict()\n",
    "            for i in range(len(word_list)):\n",
    "                word = word_list[i]\n",
    "                if word in sen_dict.keys() and word not in not_word_list and word not in degree_dict.keys():\n",
    "                    sen_word[i] = sen_dict[word]\n",
    "                elif word in not_word_list and word not in degree_dict.keys():\n",
    "                    not_word[i] = -1\n",
    "                elif word in degree_dict.keys():\n",
    "                    degree_word[i] = degree_dict[word]\n",
    "            return sen_word, not_word, degree_word\n",
    "        def count_score(sen_word, not_word, degree_word, seg_result):\n",
    "            w = 1\n",
    "            score = 0\n",
    "            sen_index = -1\n",
    "            senIndex_list = list(sen_word.keys())\n",
    "            for i in range(0, len(seg_result)):\n",
    "                if i in sen_word.keys():\n",
    "                    score += w * float(sen_word[i])\n",
    "                    sen_index += 1\n",
    "                    if sen_index < len(senIndex_list)-1:\n",
    "                        for j in range(senIndex_list[sen_index],senIndex_list[sen_index + 1]):\n",
    "                            if j in not_word.keys():\n",
    "                                w *= -1\n",
    "                            elif j in degree_word.keys():\n",
    "                                w *= float(degree_word[j])\n",
    "    return score\n",
    "def sentiment_score(sentence):\n",
    "    seg_list = cut_word(sentence)\n",
    "    sen_word, not_word, degree_word = classify_words(seg_list)\n",
    "    score = count_score(sen_word, not_word, degree_word, seg_list)\n",
    "    return score\n",
    "if __name__ == '__main__':\n",
    "    print(\"文本'今天天气晴朗，我非常开心地买了一束鲜艳的花'的情感得分：\", sentiment_score('今天天气晴朗，我非常开心地买了一束鲜艳的花'))\n",
    "    print(\"文本'天阴沉沉的，荒无人烟的路边是破旧不堪的老房子'的情感得分：\", sentiment_score('天阴沉沉的，荒无人烟的路边是破旧不堪的老房子'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
